package com.fuyu.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyu.common.utils.TreeUtil;
import com.fuyu.system.dao.SysMenuDao;
import com.fuyu.system.entity.SysMenu;
import com.fuyu.system.models.search.SysMenuSearch;
import com.fuyu.system.models.vo.menu.SysMenuListTreeVO;
import com.fuyu.system.models.vo.menu.SysMenuRouterVO;
import com.fuyu.system.service.BaseCrud;
import com.fuyu.system.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单权限表(SysMenu)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 17:38:48
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService, BaseCrud<SysMenu> {

    @Override
    public List<SysMenuListTreeVO> getMenuPageList(SysMenuSearch params) {
        List<SysMenu> sysMenus = this.list();

        List<SysMenuListTreeVO> sysMenuListTreeVOS = sysMenus.stream().map(SysMenuListTreeVO::toVO).toList();


        List<SysMenuListTreeVO> listTreeVOS = TreeUtil.builder(
                sysMenuListTreeVOS,
                SysMenu::getMenuId,
                SysMenu::getParentId,
                SysMenuListTreeVO::getChildren,
                SysMenuListTreeVO::setChildren
        );


        return listTreeVOS;
    }

    @Override
    public List<SysMenuRouterVO> getTree(String userId) {

        List<SysMenu> sysMenus = this.list(
                getLambdaQueryWrapper()
                        .in(SysMenu::getMenuType, "M", "C")
        );

        List<SysMenuRouterVO> menuRouterVOS = sysMenus.stream().map(SysMenuRouterVO::toVO).toList();

        List<SysMenuRouterVO> routerVOS = TreeUtil.builder(
                menuRouterVOS,
                SysMenuRouterVO::getMenuId,
                SysMenuRouterVO::getParentId,
                SysMenuRouterVO::getChildren,
                SysMenuRouterVO::setChildren
        );

        return routerVOS;
    }
}

